package com.wty.learnsping.webMvcConfig;

import com.wty.learnsping.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/*.filter");
    }

    /**
     * url映射到视图
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("视图路径").setViewName("视图名");
    }

    /**
     * 跨域访问配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //允许对所有请求的跨域访问
        registry.addMapping("/**")
                .allowedOrigins("http://www.baidu.com")//提供更加精确的控制
                .allowedMethods("POST","GET");


    }

    /**
     * 格式化 对于日期类型的参数，将传来的字符串转换成日期类型
     * 这里的yyyy-MM-dd HH:mm:ss是定义的字符串的格式
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        //registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
        registry.addFormatter(new DateFormatter("yyyyMMdd"));
    }

    /**
     * 静态资源的访问处理
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }
}
